package com.jzo2o.customer.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jzo2o.api.publics.SmsCodeApi;
import com.jzo2o.common.constants.CommonStatusConstants;
import com.jzo2o.common.constants.ErrorInfo;
import com.jzo2o.common.constants.UserType;
import com.jzo2o.common.enums.SmsBussinessTypeEnum;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.utils.IdUtils;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.dto.request.InstitutionRegisterReqDTO;
import com.jzo2o.customer.service.IInstitutionServe;
import com.jzo2o.customer.service.IServeProviderService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.Collection;
import java.util.Map;
import java.util.function.Function;

import static com.baomidou.mybatisplus.core.toolkit.Wrappers.lambdaQuery;


@Service
public class InstitutionServeImpl implements IInstitutionServe {
    @Resource
    private SmsCodeApi smsCodeApi;
    @Resource
    private IServeProviderService serveProviderService;
    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public void register(InstitutionRegisterReqDTO institutionRegisterReqDTO) {
        //数据校验
        if (StringUtils.isEmpty(institutionRegisterReqDTO.getVerifyCode())) {
            throw new BadRequestException("验证码错误，请重新输入");
        }
        //远程调用publics服务校验验证码是否正确
        boolean verifyResult = smsCodeApi.verify(institutionRegisterReqDTO.getPhone(), SmsBussinessTypeEnum.INSTITION_REGISTER, institutionRegisterReqDTO.getVerifyCode()).getIsSuccess();
        if (!verifyResult) {
            throw new BadRequestException("验证码错误，请重新输入");
        }
        //直接调用服务提供者服务判断该机构是否已经注册
//      serveProviderService.add(institutionRegisterReqDTO.getPhone(), UserType.WORKER, institutionRegisterReqDTO.getPassword());
        // 校验手机号是否存在
        ServeProvider existServeProvider = serveProviderService.findByPhoneAndType(institutionRegisterReqDTO.getPhone(), UserType.INSTITUTION);
        if (existServeProvider != null) {
            throw new BadRequestException("该手机号已注册");
        }
        ServeProvider serveProvider = new ServeProvider();
        serveProvider.setPhone(institutionRegisterReqDTO.getPhone());
        serveProvider.setType(UserType.INSTITUTION);
        serveProvider.setPassword(passwordEncoder.encode(institutionRegisterReqDTO.getPassword()));
        serveProvider.setStatus(CommonStatusConstants.USER_STATUS_NORMAL);
        serveProvider.setCode(IdUtils.getSnowflakeNextIdStr());
        BaseMapper<ServeProvider> baseMapper = serveProviderService.getBaseMapper();
        baseMapper.insert(serveProvider);
    }
}
